package com.juanable.niubility.designPatterns;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;

/**
 * @Auther: lyg
 * @Data: 2022/9/15
 */
@Component
public class Statement2Client {

    /**
     * 提前加载的容器(key, value 根据业务设定参数)
     */
    private static Map<String, Function<String,Boolean>> eventTypeMap = new HashMap<>();

    /**
     *  MsgType = event的类型
     *  初始化业务分派逻辑,代替了if-else部分
     *  key: 事件类型
     *  value: lambda表达式,执行的事件类型
     */
    @PostConstruct
    public void dispatcherInit(){
        // 提前添加方法
        eventTypeMap.put(StrategyEnum.TESLA.getDesc(), param -> this.tesla(param));
        eventTypeMap.put(StrategyEnum.TOYOTA.getDesc(), param -> this.toyota(param));
    }

    public void execute(String str) {
        Boolean apply = eventTypeMap.get(str).apply(str);
        System.out.println("执行:" + apply);
    }

    private Boolean tesla(String str) {
        System.out.println("辉drive:" + str);
        return Boolean.TRUE;
    }

    private Boolean toyota(String str) {
        System.out.println("杰哥drive:" + str);
        return Boolean.TRUE;
    }

}
